# -*- coding: utf-8 -*-
# @Time        :2025/4/7 17:28
# @Author      :文刀水寿
# @File        : views.py
"""
 @Description :路由 +  视图函数
"""
# 蓝图
from flask import Blueprint
from .models import *
import random

blue = Blueprint('user', __name__)


@blue.route('/')
def index():
    return 'index'


# 增加数据
@blue.route('/addstu')
def add_room_stu():
    rooms = []
    for i in range(10):
        room = Room()
        room.name = '刘涛' + str(random.randint(10, 99))
        rooms.append(room)
    try:
        db.session.add_all(rooms)
        db.session.commit()
    except Exception as e:
        print('e:', e)
        db.session.rollback()
        db.session.flush()

    return 'OK!!'



# 增加学生数据
@blue.route('/addstudent')
def add_stu():
    # 添加学生
    students = []
    for i in range(10):
        stu = Student()
        stu.name = '刘涛' + str(random.randint(10, 99))
        stu.age = i
        stu.roomid = random.randint(8, 17)
        students.append(stu)

    try:
        db.session.add_all(students)
        db.session.commit()
    except Exception as e:
        print('e:', e)
        db.session.rollback()
        db.session.flush()

    return 'OK!!'

# 修改
@blue.route('/update')
def update_stu():
    # 修改
    stu = Student.query.first()
    stu.age = 1000
    try:
        db.session.commit()
    except Exception as e:
        print('e:', e)
        db.session.rollback()
        db.session.flush()

    return 'OK!!'

# 删除
@blue.route('/delete')
def delete_stu():
    # 删除
    stu = Student.query.first()
    db.session.delete(stu)
    try:
        db.session.commit()
    except Exception as e:
        print('e:', e)
        db.session.rollback()
        db.session.flush()

    return 'OK!!'


